﻿unit LofinFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsCore, dxSkinLondonLiquidSky, dxSkinscxPCPainter, cxContainer, cxEdit,
  dxLayoutcxEditAdapters, dxLayoutControl, Menus, StdCtrls, cxButtons,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, DB, FIBDataSet, pFIBDataSet,IniFiles, cxPropertiesStore,
  dxLayoutLookAndFeels, dxSkinMcSkin, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinSeven, dxSkinBlack,
  dxSkinBlue, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinFoggy, dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinPumpkin, dxSkinSharp, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinXmas2008Blue;

type
  TLoginForm = class(TForm)
    lGrp: TdxLayoutGroup;
    lcontr1: TdxLayoutControl;
    lGrp1: TdxLayoutGroup;
    lGrp2: TdxLayoutGroup;
    cbb_user: TcxLookupComboBox;
    Item_cbUser: TdxLayoutItem;
    edt_pass: TcxTextEdit;
    Item_pass: TdxLayoutItem;
    btn_Ok: TcxButton;
    Item_bntOk: TdxLayoutItem;
    btn_cancel: TcxButton;
    Item_btnCancel: TdxLayoutItem;
    lGrp3: TdxLayoutGroup;
    DS1: TpFIBDataSet;
    ds2: TDataSource;
    cbb_base: TcxComboBox;
    ItemBase: TdxLayoutItem;
    prop1: TcxPropertiesStore;
    LaFls1: TdxLayoutLookAndFeelList;
    LafScin1: TdxLayoutSkinLookAndFeel;
    dxlytcxlkndfl1: TdxLayoutCxLookAndFeel;
    procedure FormCreate(Sender: TObject);
    procedure btn_OkClick(Sender: TObject);
    procedure btn_cancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure edt_passKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure cbb_basePropertiesChange(Sender: TObject);
  private
    { Private declarations }
    function GetDbName(Sect :string) :string;
    function ScanPassword: boolean;
    procedure LoadSettings(ini :TIniFile);
    procedure LoadUsers;
    function ConnectDb(basename :string) :Boolean;
  public
    { Public declarations }
    ini :TIniFile;
  end;

var
  LoginForm: TLoginForm;

implementation

uses  cmUtils, DModule;

{$R *.dfm}

procedure TLoginForm.btn_cancelClick(Sender: TObject);
begin
 ModalResult:=2;
end;

procedure TLoginForm.btn_OkClick(Sender: TObject);
begin
 if ScanPassword then
 begin
  CurrUser:=cbb_user.EditValue;
  UserCod:=cbb_user.EditValue;
  DbName:=cbb_base.Text;
  IsChief:=Copy(DS1.FN('PROFILES').AsString,2,1)='1';
  fProfiles:=DS1.FN('PROFILES').AsString;
  if ConnectDb(VarToStrDef(cbb_base.EditValue,'')) then
  ModalResult:=1 else
  ModalResult:=2;
 end else
 begin
   ModalResult:=mrNone;
   ShowMessage('Ошибка при вводе пароля.');
 end;
end;

procedure TLoginForm.cbb_basePropertiesChange(Sender: TObject);
begin
   LoadUsers;
end;

procedure TLoginForm.edt_passKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if Key=VK_RETURN then
 btn_OkClick(nil);
end;

function TLoginForm.ScanPassword: boolean;
var
  AValue :string;
begin
  with cbb_user.Properties.Grid.DataController do
    AValue := Values[FocusedRecordIndex, 2];
  Result:=AValue=edt_pass.Text;
end;

procedure TLoginForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 Ini.Free;
end;

procedure TLoginForm.FormCreate(Sender: TObject);
var
  FileName :string;
begin
  FileName:=ExtractFilePath(Application.ExeName)+'config\conf.ini';
  if FileExists(FileName) then
  begin
    Ini := TIniFile.Create(FileName);
    try
      LoadSettings(Ini);
      LoadForm(prop1,Self.Name);
      //LoadUsers;
    finally
      //Ini.Free;
    end;
  end else
  begin
    ShowMessage('Не найден файл конфигурации!');
    ModalResult:=2;
  end;
end;

function TLoginForm.ConnectDb(basename :string) :Boolean;
begin
  if basename<>'' then
  begin
      if dm1.db1.Connected=true then dm1.db1.Close;
      dm1.db1.DatabaseName:=GetDbName(basename);
      dm1.db1.ConnectParams.UserName:='sysdba';
      dm1.db1.ConnectParams.Password:='masterkey';
      //RK.db1.LibraryName:='fbclient.dll';
      dm1.db1.SQLDialect:=3;
      try
        dm1.db1.Connected:=True;
        Result:=True;
      Except
        on E : Exception do
        begin
          ShowMessage('Не возможно подключится к базе данных!'#10#13+
                       E.Message);
          btn_Ok.Enabled:=False;
          Result:=False;
        end;
      end;
  end;
end;

procedure TLoginForm.LoadUsers;
var
  basename: string;
  usercod :string;
begin
  basename:=VarToStrDef(cbb_base.EditValue,'');
  if basename<>'' then
  begin
      try
        if ConnectDb(basename) then
        begin
          DS1.SelectSQL.Text:=
                  'select code,name,psw,profiles from spr$users order by name';
          DS1.CloseOpen(false);
        end;
      finally

      end;
  end;
end;

function TLoginForm.GetDbName(Sect :string) :string;
var
  FileName :string;
begin
  FileName:=ExtractFilePath(Application.ExeName)+'config\conf.ini';
  if FileExists(FileName) then
  begin
    Ini := TIniFile.Create(FileName);
    try
      Result:= ini.ReadString('Base',Sect,'');
    finally

    end;
  end else
  Result:='';
end;


procedure TLoginForm.LoadSettings(ini :TIniFile);
begin
  ini.ReadSection('Base',cbb_base.Properties.Items);
end;

end.
